package org.hbnu.xiaomi.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.hbnu.xiaomi.entity.Goods;
import org.hbnu.xiaomi.entity.Orders;
import org.hbnu.xiaomi.service.GoodsService;
import org.hbnu.xiaomi.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class OrdersController {
    //注入一个订单数据服务类
    @Autowired
    OrdersService ordersService;

    //注入一个商品数据服务类
    @Autowired
    GoodsService goodsService;


    //用户下单功能
    //用户下单成功，返回:ok，失败返回:fail
    @RequestMapping("/buy")
    @ResponseBody
    public String buy(HttpServletRequest request, @RequestBody Map<String, String> map) {

        //获取要购买的商品编号
        int good_id = Integer.parseInt(map.get("good_id"));

        // 获取要购买的商品的个数
        String numStr = map.get("num");
        if (numStr == null || numStr.trim().isEmpty()) {
            // 处理错误情况，比如返回错误信息或抛出异常
            throw new IllegalArgumentException("商品数量不能为空");
        }
        int num = Integer.parseInt(numStr);

        //获取支付方法
        int paymentMethod = Integer.parseInt(map.get("paymentMethod"));

        //调试
        System.out.println("获取的参数：" + good_id + "," + num + "," + paymentMethod);
        //用户进行检查用户是否在线
        Integer user_id = (Integer) request.getSession().getAttribute("user_id");
        if (request.getSession().getAttribute("user_id") == null) {
            return "用户没有登录！";
        }
        //对商品存在进行检查
        Goods goods = goodsService.getById(good_id);
        if (goods == null) {
            return "你购买的商品不存在";
        }
        if (num<=0) {
            return "无效输入";
        }

        //库存检查
        if (!(goods.getStock() >= num)) {
            return "库存不足！";
        }

        //创建订单
        Orders orders=new Orders();
        orders.setGood_id(good_id);
        orders.setUser_id(user_id);
        orders.setNum(num);
        orders.setOrdertime(new Timestamp(System.currentTimeMillis()));
        orders.setPrice(new BigDecimal(num * goods.getPrice()));
        orders.setPic(goods.getPic());
        orders.setPaid(1);
        orders.setPaymentMethod(paymentMethod);

        //修改商品库存
        goods.setStock(goods.getStock() - num);
        goodsService.update(goods);

        //保存订单
        ordersService.insert(orders);
        return "ok";

    }
    //查看我的订单
    @RequestMapping("/getOrdersByUsersId")
    @ResponseBody
    public List<Orders>getMyOrders(HttpServletRequest request, @RequestBody Map<String, String> map) {
        //用户进行检查用户是否在线
        Integer user_id = (Integer) request.getSession().getAttribute("user_id");
        if (request.getSession().getAttribute("user_id") == null) {
            System.out.println("用户没登录！");
            return null;
        }
        //返回我的订单
        List<Orders> ordersList = ordersService.selectOrdersByUserId(user_id);

        //给每一个订单独立设置订单的商品外键
        for (Orders orders : ordersList) {
            Goods goods = goodsService.getById(orders.getGoodsId());
            orders.setGoods(goods);
        }

        return ordersList;
    }
    //确认收货
    @RequestMapping("/Receipt_confirmedd")
    @ResponseBody
    public String Receipt_confirmedd(@RequestBody Map<String, String> map) {
        int id=Integer.parseInt(map.get("id"));
        ordersService.updateOrder(id, 2);

        return "已签收";
    }


    //确认退货
    @RequestMapping("/product")
    @ResponseBody
    public String product(@RequestBody Map<String, String> map) {
        int id=Integer.parseInt(map.get("id"));
        ordersService.updateOrder(id, 3);

        return "已退货";
    }


    //删除订单
    @RequestMapping("/deleteOrder")
    @ResponseBody
    public String deleteOrder(  @RequestBody Map<String, String> map) {
        // 获取要删除的订单ID
        int id = Integer.parseInt(map.get("id"));

        // 删除订单
        try {
            ordersService.deleteById(id);
            return "删除成功";
        } catch (Exception e) {
            // 日志记录异常信息
            e.printStackTrace();
            return "删除失败";
        }
    }


}
